package com.hamu.service.impl;

import com.hamu.mapper.ArticleMapper;
import com.hamu.mapper.ArticleTagMapper;
import com.hamu.mapper.TagMapper;
import com.hamu.pojo.Article;
import com.hamu.pojo.Tag;
import com.hamu.service.ArticleService;
import com.hamu.service.ArticleTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class ArticleTagServiceImpl implements ArticleTagService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private ArticleTagMapper articleTagMapper;

    @Transactional

    @Override
    public void addTagToArticle(Integer articleId, List<Integer> tagIds) {
        // 参数校验
        if (tagIds == null || tagIds.isEmpty()) {
            throw new IllegalArgumentException("标签ID列表不能为空");
        }

        // 校验标签是否存在
        List<Tag> existingTags = tagMapper.findTagsByIds(tagIds);
        if (existingTags.size() != tagIds.size()) {
            throw new RuntimeException("包含不存在的标签");
        }

        // 批量校验是否已关联
        if (articleTagMapper.existsRelation(articleId, tagIds)) {
            throw new RuntimeException("部分标签已关联到文章");
        }

        // 批量插入关联
        articleTagMapper.insertRelations(articleId, tagIds);
    }


    @Override
    @Transactional
    public void removeTagFromArticle(Integer articleId, List<Integer> tagIds) {
        // 参数校验
        if (tagIds == null || tagIds.isEmpty()) {
            throw new IllegalArgumentException("标签ID列表不能为空");
        }

        // 批量校验关联是否存在
        if (!articleTagMapper.existsRelation(articleId, tagIds)) {
            throw new RuntimeException("文章未完全关联指定标签");
        }

        // 批量删除关联
        articleTagMapper.deleteRelations(articleId, tagIds);
    }

    @Override
    public List<Article> getArticlesByTag(List<String> tagNames, List<Integer> tagIds) {
        if ((tagIds == null || tagIds.isEmpty()) && (tagNames == null || tagNames.isEmpty())) {
            throw new IllegalArgumentException("必须提供至少一个标签ID或标签名称");
        }
        return articleTagMapper.selectArticlesByTag(tagNames, tagIds);
    }

    @Override
    @Transactional
    public void removeAllTagsFromArticle(Integer articleId) {
        // 参数校验
        if (articleId == null || articleId <= 0) {
            throw new IllegalArgumentException("无效的文章ID");
        }

        // 校验文章是否存在关联
        if (!articleTagMapper.existsRelationByArticleId(articleId)) {
            throw new RuntimeException("该文章没有关联任何标签");
        }

        // 删除所有关联
        articleTagMapper.deleteAllRelationsByArticleId(articleId);
    }

    @Override
    @Transactional
    public void updateArticleTags(Integer articleId, List<Integer> newTagIds) {
        // 参数校验
        if (articleId == null || articleId <= 0) {
            throw new IllegalArgumentException("无效的文章ID");
        }
        if (newTagIds == null || newTagIds.isEmpty()) {
            throw new IllegalArgumentException("标签ID列表不能为空");
        }

        // 校验标签是否存在
        List<Tag> existingTags = tagMapper.findTagsByIds(newTagIds);
        if (existingTags.size() != newTagIds.size()) {
            throw new RuntimeException("包含不存在的标签");
        }

        // 启动事务：先删除所有旧标签，再添加新标签
        try {
            // 删除所有旧标签
            if (articleTagMapper.existsRelationByArticleId(articleId)) {
                articleTagMapper.deleteAllRelationsByArticleId(articleId);
            }

            // 添加新标签
            articleTagMapper.insertRelations(articleId, newTagIds);
        } catch (Exception e) {
            throw new RuntimeException("标签更新失败: " + e.getMessage());
        }
    }

}
